package com.yuchting.dinneronline.client;

import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.yuchting.dinneronline.client.common.CommonDialog;
import com.yuchting.dinneronline.client.common.User;
import com.yuchting.dinneronline.client.common.YuchJSONObject;
import com.yuchting.dinneronline.share.ShareField;

public class MainPanel extends TabPanel {
	
	private static User fsm_user 	= new User();
	
	
	public final  MainPage m_mainPage;
	
	final OrderPanel m_orderPanel;
	DinnerPanel m_dinnerPanel 		= null;
	ConfigAccPanel m_configAccPanel = null;
	OrderOutPanel m_orderOutPanel	= null;
	
	public MainPanel(MainPage _main){
		m_mainPage = _main;
		
		RootPanel t_root = RootPanel.get("orderTab");
		setWidth(CommonDialog.getElementWidth(t_root.getElement()));
		t_root.add(this);
		
		m_orderPanel	= new OrderPanel(this);
		add(m_orderPanel,"订餐");
		
		setAnimationEnabled(true);
	}
	
	// get the singleton user
	public static User getCurrUser(){return fsm_user;} 
	
	public void show(String _user)throws Exception{
		
		YuchJSONObject t_json = new YuchJSONObject(_user);
		getCurrUser().fromJSON(t_json);
		
		m_orderPanel.requestData();
		
		if(ShareField.isAdmin(getCurrUser().getName())){
			
			if(m_orderOutPanel == null){
				m_orderOutPanel = new OrderOutPanel(this);
			}
			
			add(m_orderOutPanel,"每日出单");
			m_orderOutPanel.requestCurrOrder();
			
			if(m_dinnerPanel == null){
				m_dinnerPanel = new DinnerPanel(this);
			}
			
			add(m_dinnerPanel,"菜单管理");
			
			if(m_configAccPanel == null){
				m_configAccPanel = new ConfigAccPanel(this);
			}
			
			add(m_configAccPanel,"配置|账户管理");
			m_configAccPanel.requestData();
		}
		
		RootPanel t_root = RootPanel.get("orderTab");
		t_root.setVisible(true);
		
		selectTab(0);
	}
	
	public void hide(){
		
		if(ShareField.isAdmin(getCurrUser().getName())){
			while(getWidgetCount() > 1){
				remove(1);
			}			
		}
		
		RootPanel t_root = RootPanel.get("orderTab");
		t_root.setVisible(false);
	}
}
